package com.example.smssender;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

/**
 * 发送记录列表页面
 */
public class SmsSendRecordListActivity extends AppCompatActivity {
    
    private RecyclerView recyclerView;
    private SmsSendRecordAdapter adapter;
    private DataManager dataManager;
    private List<SmsSendRecord> records;
    private LinearLayout llEmptyState;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_send_record_list);
        
        dataManager = DataManager.getInstance(this);
        
        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        
        // 初始化视图
        recyclerView = findViewById(R.id.recyclerView);
        llEmptyState = findViewById(R.id.llEmptyState);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        
        // 初始化适配器
        records = dataManager.getSendRecords();
        adapter = new SmsSendRecordAdapter(records, new SmsSendRecordAdapter.OnRecordClickListener() {
            @Override
            public void onRecordClick(SmsSendRecord record) {
                // 打开详情页
                Intent intent = new Intent(SmsSendRecordListActivity.this, SmsSendRecordDetailActivity.class);
                intent.putExtra("record_id", record.getId());
                startActivity(intent);
            }
            
            @Override
            public void onRecordDelete(SmsSendRecord record) {
                // 删除记录
                deleteRecord(record);
            }
        });
        recyclerView.setAdapter(adapter);
        
        updateEmptyState();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // 刷新列表
        refreshRecords();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_send_record_list, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        } else if (id == R.id.action_clear_all) {
            clearAllRecords();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    private void refreshRecords() {
        records = dataManager.getSendRecords();
        adapter.updateRecords(records);
        updateEmptyState();
    }
    
    private void updateEmptyState() {
        if (records == null || records.isEmpty()) {
            recyclerView.setVisibility(View.GONE);
            llEmptyState.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            llEmptyState.setVisibility(View.GONE);
        }
    }
    
    private void deleteRecord(SmsSendRecord record) {
        if (record == null || record.getId() == null) {
            Toast.makeText(this, "记录信息无效", Toast.LENGTH_SHORT).show();
            return;
        }
        new AlertDialog.Builder(this)
                .setTitle("删除记录")
                .setMessage("确定要删除这条发送记录吗？")
                .setPositiveButton("删除", (dialog, which) -> {
                    dataManager.deleteSendRecord(record.getId());
                    refreshRecords();
                    Toast.makeText(this, "记录已删除", Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("取消", null)
                .show();
    }
    
    private void clearAllRecords() {
        if (records == null || records.isEmpty()) {
            Toast.makeText(this, "没有可删除的记录", Toast.LENGTH_SHORT).show();
            return;
        }
        
        new AlertDialog.Builder(this)
                .setTitle("清空所有记录")
                .setMessage("确定要清空所有发送记录吗？此操作不可恢复。")
                .setPositiveButton("清空", (dialog, which) -> {
                    dataManager.clearSendRecords();
                    refreshRecords();
                    Toast.makeText(this, "所有记录已清空", Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("取消", null)
                .show();
    }
}

